import API from '../../api/api.js'
const FORMAT = require('../../api/util.js')
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    biMing: ""
  },

  ready() {
    this.data.screenHeight = app.globalData.screenHeight
    let userInfo = wx.getStorageSync('userInfo')
    let isLogin = wx.getStorageSync('isLogin')
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    this.setData({
      isManager: userInfo.isManager,
      screenHeight: this.data.screenHeight,
      isLogin:isLogin
    })
    wx.hideLoading()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onGetUserInfo: function (e) {
      if (e.detail.errMsg === "getUserInfo:ok") {
        API.getOpenID().then(res => {
          app.globalData.openid = res
          API.getUserDataByOpenId(res).then(res => {
            if (res.length == 0) {
              let userData = {
                gender: e.detail.userInfo.gender
              }
              API.initUserInfo(userData).then(res => {
                API.getUserDataByOpenId(app.globalData.openid).then(res => {
                  this.data.isLogin = true
                  wx.setStorageSync('isLogin', true)
                  wx.setStorageSync('userInfo', res[0])
                  this.setData({
                    isLogin: true
                  })
                })
              })
            } else {
              wx.setStorageSync('isLogin', true)
              wx.setStorageSync('userInfo', res[0])
              this.setData({
                isLogin: true
              })
            }
            wx.hideLoading()
            wx.showToast({
              title: '授权成功！',
            })
          })
        })
      }
    },

    onSetBiMing: function (e) {
      this.setData({
        show: true
      })
    },

    onClose() {
      this.setData({
        show: false
      })
    },

    onGetSystemNotice: function (e) {
      wx.navigateTo({
        url: "/pages/systemnotice/systemnotice",
      })
    },
    onGetMyShiGuangPing: function (e) {
      wx.navigateTo({
        url: "/pages/myshiguangping/myshiguangping",
      })
    },
    onGetMyWenZhang: function (e) {
      wx.navigateTo({
        url: "/pages/mywenzhang/mywenzhang",
      })
    },
    onGetHelpInfo: function (e) {
      API.getDataById("wenzhangbiao", '6518b7395f5dcc800169788d6823ab12').then(res => {
        let data = {
          neiRong: res[0].neiRong,
          type: res[0].tiMu
        }
        wx.setStorageSync('currContent', data)
        wx.navigateTo({
          url: "/pages/conentdisplay/conentdisplay",
        })
      })
    },
    onEditBiMing: function (e) {
      let userInfo = wx.getStorageSync('userInfo')
      if (this.data.biMing != "") {
        API.updateBiMing(this.data.biMing).then(res => {
          userInfo.biMing = this.data.biMing
          wx.setStorageSync('userInfo', userInfo)
          wx.showToast({
            title: '笔名更新成功。',
          })
          this.setData({
            show: false
          })
        })
      }
    },

    onChange(e) {
      this.data.biMing = e.detail
    },

    onSendNotice: function (e) {
      wx.navigateTo({
        url: "/pages/sendnotice/sendnotice",
      })
    }
  }
})